package dypay

import (
	"encoding/json"
	"errors"
	"fmt"

	"gitee.com/liuxuezhan/ar-platform/pay-channel-packet/dypay/dypay/tool"
)

type WithdrawParam struct {
	MerOrderNo string `json:"merOrderNo"`
	OrderNo    string `json:"orderNo"`
	Status     int32  `json:"status"`
	SubCode    string `json:"subCode"`
	SubMsg     string `json:"subMsg"`
	Sign       string `json:"sign"`
}

type WithdrawResp struct {
	Code int           `json:"code"`
	Msg  string        `json:"msg"`
	Data WithdrawParam `json:"data"`
}
type PayResp struct {
	Code int32  `json:"code"`
	Msg  string `json:"msg"`
	Data Param  `json:"data"`
}

type Param struct {
	PayUrl     string `json:"payurl"`     // 支付页面地址，跳转即可
	MerNo      string `json:"merNo"`      // 商户单号
	SysOrderNo string `json:"orderNo"`    // 平台订单号
	MerOrderNo string `json:"merOrderNo"` // 商户订单号
	SubCode    string `json:"subCode"`    // 业务错误码
	SubMsg     string `json:"subMsg"`     // 业务错误信息
	Status     int32  `json:"status"`     // 订单状态
	OrderData  string `json:"支付链接"`       // 订单状态
	Sign       string `json:"sign"`       // 签名
}

// 代收（充值） dyPay
func DyPay(params map[string]any) (resp PayResp, err error) {
	tempParams := make(map[string]any)
	tempParams["merNo"] = params["merNo"]
	tempParams["merOrderNo"] = params["merOrderNo"]
	tempParams["name"] = params["name"]
	tempParams["email"] = params["email"]
	tempParams["phone"] = params["phone"]
	tempParams["orderAmount"] = params["orderAmount"]
	tempParams["currency"] = params["currency"]
	tempParams["busiCode"] = params["busiCode"]
	tempParams["pageUrl"] = params["pageUrl"]
	tempParams["notifyUrl"] = params["notifyUrl"]
	tempParams["timestamp"] = params["timestamp"]
	v := tool.Sign(tempParams, params["SecretKey"].(string))
	params["sign"] = v
	output, err := tool.SendPostJson(params["url"].(string), params)
	if err != nil {
		return resp, err
	}
	var result PayResp
	err = json.Unmarshal([]byte(output), &result)
	if err != nil {
		return resp, err
	}
	if result.Code != 1 {
		return resp, errors.New(result.Msg)
	}
	return result, nil
}

// 代付（提现）Withdraw
func Withdraw(params map[string]any) (resp WithdrawResp, err error) {
	// 请求第三方接口
	tempParams := make(map[string]any)
	tempParams["bankCode"] = params["bankCode"]
	tempParams["identityType"] = params["identityType"]
	tempParams["identityNo"] = params["identityNo"]
	tempParams["accName"] = params["accName"]
	tempParams["accNo"] = params["accNo"]
	tempParams["busiCode"] = params["busiCode"]
	tempParams["currency"] = params["currency"]
	tempParams["email"] = params["email"]
	tempParams["merNo"] = params["merNo"]
	tempParams["merOrderNo"] = params["merOrderNo"]
	tempParams["notifyUrl"] = params["notifyUrl"]
	tempParams["orderAmount"] = params["orderAmount"]
	tempParams["phone"] = params["phone"]
	tempParams["timestamp"] = params["timestamp"]
	v := tool.Sign(tempParams, params["SecretKey"].(string))
	params["sign"] = v
	data, err := tool.SignData(v, params["PrivateKey"].(string))
	if err != nil {
		return WithdrawResp{}, err
	}
	fmt.Println(data)
	output, err := tool.SendPostFormUrlEncoded(params["url"].(string), tempParams)
	if err != nil {
		return resp, fmt.Errorf("sendErr:%v", err)
	}

	var result WithdrawResp
	err = json.Unmarshal([]byte(output), &result)
	if err != nil {
		return result, fmt.Errorf("json.Unmarshal:%v", err, output)
	}
	if result.Code != 1 {
		return result, fmt.Errorf("code != 1:%v", err)
	}
	return result, nil
}
